package tcpFirmware.controller;

import java.io.IOException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import tcpFirmware.ResponseBody;
import tcpFirmware.Routes;
import tcpFirmware.Type;
import tcpFirmware.model.DBHelper;

public class ChangeController {
	@Routes(route = "change")
	public void change(Socket socket, HashMap<String,String> head,HashMap<String,String> form) {

		String oldpassword=form.get("oldPassword");
		String newpassword=form.get("newPassword");
//		String confirmpassword=form.get("ConfirmPassword");
		String username=form.get("username");
		Connection connection = new DBHelper().getConnection();
		try {
			Statement statement =connection.createStatement();
			ResultSet rs = statement.executeQuery("select* from Users where username='"+username+"'");
			rs.next();
			if(rs.getString("password").equals(oldpassword)) {
				ResponseBody responseBody = new ResponseBody(socket);				
				int i=statement.executeUpdate("update Users set password='"+newpassword+"' where password='"+oldpassword+"' and username='"+username+"'");
				if(i==1) {
					responseBody.setHeader("修改状态","修改成功").setType(Type.text.type);
				}else {
					responseBody.setHeader("修改状态","修改失败").setType(Type.text.type);
				}
				responseBody.sendResponse();
			}
		} catch (SQLException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
